<template>
	<xn-form-container
		:title="formData.id ? '编辑报修申请' : '增加报修申请'"
		:width="700"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-row :gutter="16">
				<a-col :span="12">
					<a-form-item label="申请时间：" name="applyDate">
						<a-date-picker v-model:value="formData.applyDate" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择申请时间" style="width: 100%" />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="状态：" name="status">
						<a-select v-model:value="formData.status" placeholder="请选择状态" :options="statusOptions" />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="标题：" name="title">
						<a-input v-model:value="formData.title" placeholder="请输入标题" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="附件：" name="attachFile">
						<a-input v-model:value="formData.attachFile" placeholder="请输入附件" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="宿舍楼：" name="buildName">
						<a-input v-model:value="formData.buildName" placeholder="请输入宿舍楼" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="联系人：" name="linkman">
						<a-input v-model:value="formData.linkman" placeholder="请输入联系人" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="联系人电话：" name="linkmanTel">
						<a-input v-model:value="formData.linkmanTel" placeholder="请输入联系人电话" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="报修类型：" name="repairType">
						<a-input v-model:value="formData.repairType" placeholder="请输入报修类型" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="房间号：" name="roomName">
						<a-input v-model:value="formData.roomName" placeholder="请输入房间号" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="希望完成时间：" name="wishDealDate">
						<a-date-picker v-model:value="formData.wishDealDate" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择希望完成时间" style="width: 100%" />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="备注：" name="remarks">
						<a-textarea v-model:value="formData.remarks" placeholder="请输入备注" :auto-size="{ minRows: 3, maxRows: 5 }" />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="是否夜间报修：" name="nightFlag">
						<a-input v-model:value="formData.nightFlag" placeholder="请输入是否夜间报修" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="预约时间段：" name="dealDateTime">
						<a-input v-model:value="formData.dealDateTime" placeholder="请输入预约时间段" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="取消原因：" name="quxiaoInfo">
						<a-textarea v-model:value="formData.quxiaoInfo" placeholder="请输入取消原因" :auto-size="{ minRows: 3, maxRows: 5 }" />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="维修项目id：" name="repairItemId">
						<a-input v-model:value="formData.repairItemId" placeholder="请输入维修项目id" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="维修项目：" name="repairItemName">
						<a-input v-model:value="formData.repairItemName" placeholder="请输入维修项目" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="宿舍楼id：" name="dormitoryId">
						<a-input v-model:value="formData.dormitoryId" placeholder="请输入宿舍楼id" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="维修工：" name="workerId">
						<a-input v-model:value="formData.workerId" placeholder="请输入维修工" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="接单时间：" name="acceptDate">
						<a-date-picker v-model:value="formData.acceptDate" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择接单时间" style="width: 100%" />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="完成时间：" name="finishDate">
						<a-date-picker v-model:value="formData.finishDate" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择完成时间" style="width: 100%" />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="意见：" name="opinion">
						<a-textarea v-model:value="formData.opinion" placeholder="请输入意见" :auto-size="{ minRows: 3, maxRows: 5 }" />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="评价时间：" name="starDate">
						<a-date-picker v-model:value="formData.starDate" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择评价时间" style="width: 100%" />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="停止时间：" name="stopDate">
						<a-date-picker v-model:value="formData.stopDate" value-format="YYYY-MM-DD HH:mm:ss" show-time placeholder="请选择停止时间" style="width: 100%" />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="评价：" name="scores">
						<a-input-number v-model:value="formData.scores" :min="1" :max="10000" style="width: 100%" />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="评价内容：" name="appraise">
						<a-textarea v-model:value="formData.appraise" placeholder="请输入评价内容" :auto-size="{ minRows: 3, maxRows: 5 }" />
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="repairApplyForm">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import repairApplyApi from '@/api/biz/repairApplyApi'
	// 抽屉状态
	const visible = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const statusOptions = ref([])

	// 打开抽屉
	const onOpen = (record) => {
		visible.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		statusOptions.value = tool.dictList('COMMON_STATUS')
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		visible.value = false
	}
	// 默认要校验的
	const formRules = {
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value.validate().then(() => {
			submitLoading.value = true
			const formDataParam = cloneDeep(formData.value)
			repairApplyApi
				.repairApplySubmitForm(formDataParam, !formDataParam.id)
				.then(() => {
					onClose()
					emit('successful')
				})
				.finally(() => {
					submitLoading.value = false
				})
		})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
